package com.hzzy.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hzzy.IDao.HzClassCourseMapper;
import com.hzzy.bean.HzClassCourse;
import com.hzzy.bean.HzClassDaycourse;
import com.hzzy.service.HzClassCourseService;
import com.hzzy.service.HzClassDaycourseService;

@Service
public class HzClassCourseServiceImpl implements HzClassCourseService {

	@Autowired
	private HzClassCourseMapper hzClassCourseMapper;
	
	/**
	 * 返回当前时间段的课程
	 * @param classId	班级id
	 * @param currentClassTimes 当前课次
	 * @return
	 */
	public HzClassCourse getCurrentCourse(int classId, int currentClassTimes) {
		Map<String, Object[]> conditions = new HashMap<String, Object[]>();
		// 当前节次
		conditions.put("currentSection", new Object[] {currentClassTimes});
		// 班级Id
		conditions.put("classId", new Object[]{classId});
		List<HzClassCourse> list = hzClassCourseMapper.selectCurrentClassCourse(conditions);
		if (list != null && list.size() > 0) {
			HzClassCourse course = list.get(0);
			
			if (course.getHzClassDaycourse() != null) {
				// 如果当前课程已经被调整，那么调课数据将在点到信息条件里面获取
				if (course.getHzClassDaycourse().getIsChangeCourse() == 1) {
					conditions = new HashMap<String, Object[]>();
					conditions.put("chechinsClassId", new Object[]{classId} );
					conditions.put("currentSection", new Object[] {currentClassTimes});
					conditions.put("currentDate", new Object[]{new Date()});
					list = hzClassCourseMapper.selectCurrentClassCourse(conditions);
					if (list != null && list.size() > 0) {
						HzClassCourse _course = list.get(0);
						if (_course.getHzClassCheckins() != null) {
							HzClassDaycourse daycourse = new HzClassDaycourse();
							String sections = _course.getHzClassCheckins().getSection();
							daycourse.setStartSection(Integer.parseInt(sections.split("-")[0]));
							daycourse.setEndSection(Integer.parseInt(sections.split("-")[1]));
							daycourse.setIsCheckins(HzClassDaycourseService.IS_CHECKINS_STATE);
							_course.setHzClassDaycourse(daycourse); 
						}
						return _course;
					}
				}
			}
			
			return course;
		}
		return null;
	}
	
	/**
	 * 根据所在的班级，以及 点到的 id 确认课程
	 * @param classId
	 * @param checkinesId
	 * @return
	 */
	public HzClassCourse getCourseByClassIdAndCheckinsId(int classId, int checkinesId) {
		Map<String, Object[]> conditions = new HashMap<String, Object[]>();
		conditions.put("chechinsClassId", new Object[] {classId});
		conditions.put("currentDate", new Object[]{new Date()});
		conditions.put("checkinsId", new Object[] {checkinesId});
		List<HzClassCourse> list = hzClassCourseMapper.selectCurrentClassCourse(conditions);
		if (list != null && list.size() > 0){
			HzClassCourse _course = list.get(0);
			if (_course.getHzClassCheckins() != null) {
				HzClassDaycourse daycourse = new HzClassDaycourse();
				String sections = _course.getHzClassCheckins().getSection();
				daycourse.setStartSection(Integer.parseInt(sections.split("-")[0]));
				daycourse.setEndSection(Integer.parseInt(sections.split("-")[1]));
				daycourse.setIsCheckins(HzClassDaycourseService.IS_CHECKINS_STATE);
				_course.setHzClassDaycourse(daycourse); 
			}
			return _course;
		}
			
		return null;
	}


	/**
	 * 	返回允许调课的课程
	 * @param classId
	 * @return
	 */
	public List<HzClassCourse> getChangeCourseData(int classId) {
		List<HzClassCourse> list = hzClassCourseMapper.selectChangeCourseData(classId);
		if (list != null) {
			return list;
		}
		return new ArrayList<HzClassCourse>();
	}
}
